#coding:utf8
#
# Author: CMGS
# Created on 2011-1-18
#

from libs.sqlpool import sqlPool as _pool

TABLE_NAME = 'rss'

def add(url = '', comment = '', provider = '', flag = 0):
    sqlCmd = '''INSERT INTO %s(source, comment, provider, flag) VALUES ('%s', '%s', '%s', '%d');'''
    sqlCmd = sqlCmd % (TABLE_NAME, url, comment, provider, flag)
    ret = _pool.execute(sqlCmd)
    if ret != 1:
        return False
    return True

def mark(url):
    sqlCmd = '''UPDATE %s SET flag = 1 WHERE source = '%s'; ''' % (TABLE_NAME, url)
    ret = _pool.execute(sqlCmd)
    if ret != 1:
        return False
    return True

def delete(url):
    sqlCmd = '''DELETE FROM %s WHERE source = '%s'; ''' % (TABLE_NAME, url)
    ret = _pool.execute(sqlCmd)
    if ret != 1:
        return False
    return True

def getVaildSources():
    sqlCmd = '''SELECT * FROM %s WHERE flag = 1;''' % TABLE_NAME
    ret = _pool.query(sqlCmd, _pool.RET_DICT_ROW_FOR_KEY)
    return ret

def getSource(url):
    sqlCmd = '''SELECT * FROM %s WHERE source = '%s';''' % (TABLE_NAME, url)
    ret = _pool.query(sqlCmd, _pool.RET_DICT_ROW_FOR_KEY)
    return ret

if __name__ == '__main__':
    add(u'http://www.cnbeta.com/backend', u'业界资讯站', u'ilskdw@gmail.com')
    add(u'http://www.verycd.com/sto/feed', u'驴子下载站', u'ilskdw@gmail.com')
    mark('http://www.cnbeta.com/backend')
    print getVaildSources()
    print getSource('http://www.cnbeta.com/backend')
    delete('http://www.cnbeta.com/backend')
    delete('http://www.verycd.com/sto/feed')    
